import requests
import pandas as pd

key = '6e1e63fc782eca8d3f782e3e1395b312'

def get_distance(start_loc, end_loc):
    url = 'https://restapi.amap.com/v5/direction/driving'
    params = {
        'key': key,
        'origin': start_loc,
        'destination': end_loc
    }
    response = requests.get(url, params=params)
    data = response.json()
    distance = data['route']['paths'][0]['distance']
    paths_count=len(data['route']['paths'])
    return distance

def get_nearby_pois(location):
    url = 'https://restapi.amap.com/v5/place/around'
    params = {
        'key': key,
        'location': location,
        'keywords': '快递网点',
        'radius': 10000,
        'page_size': 25,
        'page_num': 1
    }
    all_pois = []
    while True:
        response = requests.get(url, params=params)
        data = response.json()
        pois = data['pois']
        all_pois.extend(pois)
        if len(pois) < 25:
            break
        params['page_num'] += 1

    return all_pois

def collect_data(destinations):
    df = pd.DataFrame(columns=['起点', '起点坐标', '目的地', '目的地坐标', '行车方案数', '最短距离'])
    for start_point in destinations:
        start_loc = ','.join(destinations[start_point]['location'])
        pois = get_nearby_pois(start_loc)
        for poi in pois:
            end_point = poi['name']
            end_loc = poi['location']
            distance = get_distance(start_loc, end_loc)
            df = df._append({
                '起点': start_point,
                '起点坐标': start_loc,
                '目的地': end_point,
                '目的地坐标': end_loc,
                '行车方案数': 1,
                '最短距离': distance
            }, ignore_index=True)
    return df

destinations = {
    '中诚大厦': {'location': ['114.285327', '30.593856']},
    '良友大厦': {'location': ['114.292385', '30.591015']},
    '武银大厦': {'location': ['114.285327', '30.593856']}
}

data = collect_data(destinations)
data.to_excel('delivery_data_gpt3.xlsx', index=False)